Matplotlib Tutorial Part 03 - Bar Charts and Histograms


In [2]:
%matplotlib inline
import matplotlib.pyplot as plt

Bar Chart


In [9]:
fig = plt.figure(figsize=(17,9))

X = [1,5,8,2,4,6,8]
Y = [1,8,2,4,8,9,3]

plt.bar(X,Y, label="Bar")

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()


Multiple Bars Chart


In [4]:
fig = plt.figure(figsize=(17,9))
X = [1,0,8,1,4,6,8]
Y = [1,8,2,4,8,9,3]

X2 = [1,3,5,9,11]
Y2 = [8,7,1,6,1]

plt.bar(X,Y, label="Bar1")
plt.bar(X2,Y2, label="Bar2")

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()


Adding different Colours


In [11]:
fig = plt.figure(figsize=(17,9))
X = [1,0,8,3,4]
Y = [1,8,2,4,8]

X2 = [1,0,8,3,4]
Y2 = [9,7,1,6,1]

plt.bar(X,Y, label="Bar1", color='r')
plt.bar(X2,Y2, label="Bar2", color='c')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()


Swapping order


In [12]:
fig = plt.figure(figsize=(17,9))
X = [1,0,8,3,4]
Y = [1,8,2,4,8]

X2 = [1,0,8,3,4]
Y2 = [9,7,1,6,1]

plt.bar(X2,Y2, label="Bar2", color='c')
plt.bar(X,Y, label="Bar1", color='r')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()


Histogram vs Bar Charts


In [13]:
from random import randint

fig = plt.figure(figsize=(17,9))

X = [randint(0,110) for i in range(100)]
Y = [x for x in range(len(X))]

plt.bar(X,Y, label="Bar", color='r')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()

fig = plt.figure(figsize=(17,9))

bins = [i for i in range(0,max(X),10)]
plt.hist(X,bins,histtype='bar',rwidth = 0.8)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Histogram')
plt.show()